﻿using MJPGStreamer;
using OpenCvSharp;

Console.WriteLine("HTTP MJPG Streamer!");

string url = "http://localhost:8080/stream/";

MjpegStreamer mjpegStreamer = new(url);

VideoCapture cap = new();

cap.Open(0);

if (!cap.IsOpened())
{
    throw new InvalidOperationException("摄像头未打开");
}

Mat frame = new();

while (true)
{
    bool success = cap.Read(frame);
    if (!success)
    {
        Console.WriteLine("读取图像帧失败，退出");
        break;
    }
    var buf = frame.ToMemoryStream(".jpeg").ToArray();

    mjpegStreamer.PushImage(buf);

    Thread.Sleep(1);
}

Console.ReadLine();
